import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Trilateral{

	private	JFrame		aFrame = new JFrame("Created By Kelompok 3");
		
	private	JPanel 		panel1 = new JPanel(),
						panel2 = new JPanel();
					
	private	JTextField	txtAlas = new JTextField(),
						txtTinggi = new JTextField();
					
	private	JLabel		lblAlas = new JLabel("Alas :"),
						lblTinggi = new JLabel("Tinggi :"),
						lblHasil = new JLabel("Hasil : "),
						lblBackground = new JLabel(new ImageIcon("bg.png"));
					
	private	JButton		btnClear = new JButton("Clear"),
						btnHasil = new JButton("Hasil"),
						btnClose = new JButton("Tutup"),
						btnSave = new JButton("Save"),
						btnUpdate = new JButton("Update"),
						btnDelete = new JButton("Delete");
						
	private Connection 	kon = null;
	
	private String[] 	judul	= {"Alas","Tinggi","Hasil"};
	
	private DefaultTableModel	dtmModel = new DefaultTableModel(null,judul);
	
	private JTable		tblSegitiga = new JTable();
	
	private JScrollPane	scrTable = new JScrollPane();
	
	
	double Luas(double a, double t){
	
		return (a*t)/2;
		
	}
	
	void Clear(){
	
		txtAlas.setText("0");
		txtTinggi.setText("0");
		lblHasil.setText("Hasil : ");
	
	}
	
	public Connection openConnection() throws SQLException{
		Connection k = null;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			k = DriverManager.getConnection("jdbc:mysql://localhost:3306/Trilateral","root","ilovemyfamily");
			return k;
		}
		catch(SQLException sqle){
			JOptionPane.showMessageDialog(null,"Gagal membuka koneksi");
			return k;
		}
		catch(Exception e){
			return k;
		}
	}
	
	public void cari(){
		try{
			Statement st = openConnection().createStatement();
			String cr = "select*from Segitiga where alas = "+txtAlas.getText();
		
			ResultSet rs = st.executeQuery(cr);
		
			if (rs.next()){
				txtTinggi.setText(Double.toString(rs.getDouble("tinggi")));
				lblHasil.setText("Hasil : "+rs.getDouble("luas"));
			}
			else{
				txtTinggi.setText("0");
				lblHasil.setText("Hasil : ");
			}
		}
		catch(SQLException sqle){
		}
	}
	
	public void save(){
		try{
			Statement st = openConnection().createStatement();
			String masuk = "insert into Segitiga(alas,tinggi,luas) values("+txtAlas.getText()+","+txtTinggi.getText()+","+Luas(Double.parseDouble(txtAlas.getText()),Double.parseDouble(txtTinggi.getText()))+")";
		
			int row = st.executeUpdate(masuk);
			
			if (row == 1){
				JOptionPane.showMessageDialog(null,"Berhasil disimpan");
			}
			else
				JOptionPane.showMessageDialog(null,"Gagal");
			
			insertTable();
		}
		catch(SQLException sqle){
			JOptionPane.showMessageDialog(null,"Gagal di try");
		}
	}
	
	public void update(){
		try{
			Statement st = openConnection().createStatement();
			String ubah = "update Segitiga set tinggi = "+txtTinggi.getText()+", Luas = "+Luas(Double.parseDouble(txtAlas.getText()),Double.parseDouble(txtTinggi.getText()))+" where alas = "+txtAlas.getText();
		
			int row = st.executeUpdate(ubah);
			
			if (row == 1){
				JOptionPane.showMessageDialog(null,"Berhasil diupdate");
			}
			else
				JOptionPane.showMessageDialog(null,"Gagal");
				
			insertTable();
		}
		catch(SQLException sqle){
			JOptionPane.showMessageDialog(null,"Gagal di try");
		}
	}

	public void delete(){
		try{
			Statement st = openConnection().createStatement();
			String hapus = "delete from Segitiga where alas = "+txtAlas.getText();
		
			int row = st.executeUpdate(hapus);
			
			if (row == 1){
				JOptionPane.showMessageDialog(null,"Berhasil dihapus");
			}
			else
				JOptionPane.showMessageDialog(null,"Gagal");
			
			insertTable();
		}
		catch(SQLException sqle){
			JOptionPane.showMessageDialog(null,"Gagal di try");
		}
	}
	
	void deleteTable(){
		for(int i = dtmModel.getRowCount()-1;i > -1;i--){
			dtmModel.removeRow(i);
		}
	}
	
	void insertTable(){
		try{
			deleteTable();
			
			Statement st = openConnection().createStatement();
			
			String cari = "select*from Segitiga";
			
			ResultSet rs = st.executeQuery(cari);
			
			while (rs.next()){
				String[] data = {""+rs.getDouble("alas"),""+rs.getDouble("tinggi"),""+rs.getDouble("Luas")};
				dtmModel.addRow(data);
			}
		}
		catch(SQLException sqle){
		}
	}
		
	public Trilateral(){
	
		Clear();
		
		ActionListener alHasil = new	ActionListener(){
											public void actionPerformed(ActionEvent e){
												double alas = Double.parseDouble(txtAlas.getText());
												double tinggi = Double.parseDouble(txtTinggi.getText());		
												double luasSegitiga = Luas(alas,tinggi);
												lblHasil.setText("Hasil : "+luasSegitiga);
											}
										};
										
		ActionListener alClear = new	ActionListener(){
											public void actionPerformed(ActionEvent e){
												Clear();
											}
										};
										
		ActionListener alClose = new	ActionListener(){
											public void actionPerformed(ActionEvent e){
												System.exit(0);
											}
										};

		ActionListener alSave = new	ActionListener(){
											public void actionPerformed(ActionEvent e){
												save();
											}
										};	
		ActionListener alUpdate = new	ActionListener(){
											public void actionPerformed(ActionEvent e){
												update();
											}
										};										
		
		ActionListener alDelete = new	ActionListener(){
											public void actionPerformed(ActionEvent e){
												delete();
											}
										};
		btnHasil.addActionListener(alHasil);
		btnClear.addActionListener(alClear);
		btnClose.addActionListener(alClose);
		btnSave.addActionListener(alSave);
		btnUpdate.addActionListener(alUpdate);
		btnDelete.addActionListener(alDelete);
										
		panel1.setLayout(null);
		
		lblAlas.setBounds(20,20,80,20);
		lblTinggi.setBounds(20,60,80,20);
		lblHasil.setBounds(60,100,200,20);
		lblBackground.setBounds(0,0,500,375);
		txtAlas.setBounds(120,20,80,20);
		txtTinggi.setBounds(120,60,80,20);
		btnSave.setBounds(280,20,100,20);
		btnUpdate.setBounds(280,60,100,20);
		btnDelete.setBounds(280,100,100,20);
		lblHasil.setOpaque(true);
		lblHasil.setFont(new Font("Arial",Font.BOLD,12));
		lblHasil.setBackground(Color.yellow);
		lblHasil.setForeground(Color.red);
		
		txtAlas.addFocusListener(new	FocusListener(){
											public void focusGained(FocusEvent fe){
											}
											public void focusLost(FocusEvent fe){
												cari();
											}
										});
		panel1.setLayout(null);
		
		tblSegitiga.setModel(dtmModel);
		scrTable.getViewport().add(tblSegitiga);
		tblSegitiga.setEnabled(true);
		
		scrTable.setBounds(20,140,450,180);

		panel1.add(lblAlas);
		panel1.add(lblTinggi);
		panel1.add(lblHasil);
		panel1.add(txtAlas);
		panel1.add(txtTinggi);
		panel1.add(btnSave);
		panel1.add(btnUpdate);
		panel1.add(btnDelete);
		panel1.add(scrTable);
		panel1.add(lblBackground);
		
		panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		btnHasil.setMnemonic('H');
		btnClear.setMnemonic('C');
		btnClose.setMnemonic('T');
		
		panel2.add(btnHasil);
		panel2.add(btnClear);
		panel2.add(btnClose);
		
		aFrame.getContentPane().add(panel1, BorderLayout.CENTER);
		aFrame.getContentPane().add(panel2, BorderLayout.SOUTH);
		
		aFrame.setSize(500,400);
		aFrame.setVisible(true);
		aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		aFrame.setLocation(300,150);
		
		insertTable();
		
	}
	
	public static void main(String[] args){
	
		new Trilateral();
		
	}
	
}